package br.com.binding.rebind.template;

import br.com.binding.rebind.ListenerTemplate;

import br.com.binding.client.bind.annotations.ActionBind;
import com.google.gwt.core.ext.typeinfo.JField;

public class ActionListenerTemplate implements ListenerTemplate {
    
  protected static String nl;
  public static synchronized ActionListenerTemplate create(String lineSeparator)
  {
    nl = lineSeparator;
    ActionListenerTemplate result = new ActionListenerTemplate();
    nl = null;
    return result;
  }

  public final String NL = nl == null ? (System.getProperties().getProperty("line.separator")) : nl;
  protected final String TEXT_1 = "addClickListener(new com.google.gwt.user.client.ui.ClickListener() {" + NL + "                            public void onClick(com.google.gwt.user.client.ui.Widget sender) {";
  protected final String TEXT_2 = NL + "                                ";
  protected final String TEXT_3 = "();" + NL + "                            }" + NL + "                        });";

    /* (non-javadoc)
     * @see IGenerator#generate(Object)
     */
    public String generate(Object argument)
  {
    final StringBuffer stringBuffer = new StringBuffer();
    
    JField field = (JField)argument;
    ActionBind bind = field.getAnnotation(ActionBind.class); 

    stringBuffer.append(TEXT_1);
    stringBuffer.append(TEXT_2);
    stringBuffer.append(bind.method());
    stringBuffer.append(TEXT_3);
    return stringBuffer.toString();
  }
} 